/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var DynaObject = new Class({
    Extends: GameObject,
    initialize: function(type){
	    this.parent();
	    this.type=type;
		this.HTMLIncarnation=new Element('div',{'class': type});
    },
    comeIntoExistence: function(world,_left,_top){
        this.parent(world);
        this.setPosition(_left,_top);
        // when they come into existence they must come at precise position
        // that is x multiple of game.standardWidth and y multiple of game.standardHeight
        
        // for debugging
        if(game.debug==1){
          if(this.x%game.standardWidth!=0)
                  alert(this.type+' x='+this.x);
          if(this.y%game.standardHeight!=0)
                  alert(this.type+' y='+this.y);
        }
    },
    setPosition: function(l,t){
        this.x=l;
        this.y=t;
        this.HTMLIncarnation.setStyle('top',this.y);
        this.HTMLIncarnation.setStyle('left',this.x);
        if($defined(this.myZone)){
            var newZone = util.getZoneAtPoint(this.x,this.y);
            if(newZone != this.myZone){
                if(!newZone.freeZone){
                   if(newZone.hasType(GAMEUNITTYPE.WALL) || newZone.hasType(GAMEUNITTYPE.FAKEWALL)){
                       if(game.debug==1)
                        alert('zone at: '+this.x+'-'+this.y+' is a wall. it has a type: '+gameZone.types());
                   }
                  }
                newZone.addElement(this);
                this.freeMyZone();
                this.myZone = newZone;
            }
        } else {
          var gameZone=util.getZoneAtPoint(this.x,this.y);
          if(!gameZone.freeZone){
               if(gameZone.hasType(GAMEUNITTYPE.WALL) || gameZone.hasType(GAMEUNITTYPE.FAKEWALL)){
                   if(game.debug==1)
                    alert('zone at: '+this.x+'-'+this.y+' is a wall. it has a type: '+gameZone.types());
               }
          }
          gameZone.addElement(this);
          this.myZone=gameZone;
        }
    },
    freeMyZone: function(){
        this.myZone.removeElement(this);
    },
    terminate: function(){
        this.freeMyZone();
        this.HTMLIncarnation.dispose();
        this.HTMLIncarnation.destroy();
    }
});
